package org.qiyu.live.im.core.server.common;

import org.qiyu.live.im.constants.ImConstants;

import java.io.Serial;
import java.io.Serializable;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-10-06
 * Time: 20:49
 */
public class ImMsg implements Serializable {

    @Serial
    private static final long serialVersionUID = 5025331035946862884L;

    // 魔数 --> 用于做消息校验的(可以理解为校验和)
    private short magic;
    // code --> 消息的标识
    private int code;
    // body的长度
    private int len;
    // body --> 存放消息的字节数组
    private byte[] body;

    /**
     * 专门用于构建ImMsg
     *
     * @param code 业务代码
     * @param data 数据
     * @return 返回ImMsg
     */
    public static ImMsg build(int code, String data) {
        ImMsg imMsg = new ImMsg();
        imMsg.setCode(code);
        imMsg.setMagic(ImConstants.DEFAULT_MAGIC);
        imMsg.setBody(data.getBytes());
        imMsg.setLen(data.length());
        return imMsg;
    }

    public short getMagic() {
        return magic;
    }

    public void setMagic(short magic) {
        this.magic = magic;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public int getLen() {
        return len;
    }

    public void setLen(int len) {
        this.len = len;
    }

    public byte[] getBody() {
        return body;
    }

    public void setBody(byte[] body) {
        this.body = body;
    }

    @Override
    public String toString() {
        return "ImMsg{" +
                "magic=" + magic +
                ", code=" + code +
                ", len=" + len +
                ", body=" + Arrays.toString(body) +
                '}';
    }
}